if (!kwok) var kwok = {};
if (!kwok.downloadpanel) kwok.downloadpanel = {};

kwok.downloadpanel =
{
	// store stuff needed later on
	tbname: null,
	tburi: null,
	tbref: null,
	tbsize: null,
	tbpath: null,
	tbstarttime: null,
	tbendtime: null,
	prefs: Components.classes["@mozilla.org/preferences-service;1"]
		.getService(Components.interfaces.nsIPrefService)
		.getBranch("extensions.downloadpanel."),
	dlmanager: Components.classes["@mozilla.org/download-manager;1"]
		.getService(Components.interfaces.nsIDownloadManager),

	/*
	 * Initialises variables.
	 */
	init: function()
	{
		// sets up initial values
		tbname = document.getElementById("download-name");
		tburi = document.getElementById("download-uri");
		tbref = document.getElementById("download-referrer");
		tbsize = document.getElementById("download-size");
		tbpath = document.getElementById("download-directory");
		tbstarttime = document.getElementById("download-starttime");
		tbendtime = document.getElementById("download-endtime");
		
		kwok.downloadpanel.prefs.QueryInterface(Components.interfaces.nsIPrefBranch2);
		
		kwok.downloadpanel.loadRows();
		
		var dml = 
		{
			onDownloadStateChange: function(aState, aDownload)
			{
				kwok.downloadpanel.showDownloadInfo();
			},
			
			onStateChange: function(aWebProgress, aRequest, aStateFlags, aStatus, aDownload)
			{
				kwok.downloadpanel.showDownloadInfo();
			},

			onProgressChange: function(aWebProgress, aRequest, aCurSelfProgress, aMaxSelfProgress, aCurTotalProgress, aMaxTotalProgress, aDownload)
			{
				kwok.downloadpanel.showDownloadInfo();
			},

			onSecurityChange: function(aWebProgress, aRequest, aState, aDownload)
			{
				kwok.downloadpanel.showDownloadInfo();
			}
		};
		
		kwok.downloadpanel.dlmanager.addListener(dml);

		window.addEventListener("unload", kwok.downloadpanel.destroy, false);
	},

	/*
	 * Cleanup code.
	 */
	destroy: function()
	{
		tbname = null;
		tburi = null;
		tbref = null;
		tbsize = null;
		tbpath = null;
		tbstarttime = null;
		tbendtime = null;
	},

	/*
	 * Shows information about the selected download.
	 */
	showDownloadInfo: function()
	{
		var rb = document.getElementById("downloadView");
		var ri = rb.selectedItem;
		
		if ((ri != null) && (rb.itemCount > 0))
		{
			tbname.value = ri.getAttribute("target");;
			tbname.setAttribute("tooltiptext", tbname.value);
			
			tburi.value = ri.getAttribute("uri");
			tburi.setAttribute("tooltiptext", tburi.value);
			
			tbref.value = ri.getAttribute("referrer");
			tbref.setAttribute("tooltiptext", tbref.value);
			
			var theMax = ri.getAttribute("maxBytes");
			if (theMax == -1)
				theMax = "???";
				
			tbsize.value = parseInt(ri.getAttribute("currBytes")).toLocaleString() + " / " + parseInt(theMax).toLocaleString() + " bytes";
			tbsize.setAttribute("tooltiptext", tbsize.value);
			
			tbpath.value = ri.getAttribute("path");
			tbpath.setAttribute("tooltiptext", tbpath.value);
			
			tbstarttime.value = new Date(parseInt(ri.getAttribute("startTime"))).toLocaleString();
			tbstarttime.setAttribute("tooltiptext", tbstarttime.value);
			
			tbendtime.value = new Date(parseInt(ri.getAttribute("endTime"))).toLocaleString();
			tbendtime.setAttribute("tooltiptext", tbendtime.value);
		}
		else
		{
			kwok.downloadpanel.showBlankInfo();
		}
	},

	/*
	 * Clears the text box text.
	 */
	showBlankInfo: function()
	{
		tbname.value = "";
		tbname.setAttribute("tooltiptext", tbname.value);
		
		tburi.value = "";
		tburi.setAttribute("tooltiptext", tburi.value);
		
		tbref.value = "";
		tbref.setAttribute("tooltiptext", tbref.value);
		
		tbsize.value = "";
		tbsize.setAttribute("tooltiptext", tbsize.value);
		
		tbpath.value = "";
		tbpath.setAttribute("tooltiptext", tbpath.value);
		
		tbstarttime.value = "";
		tbstarttime.setAttribute("tooltiptext", tbstarttime.value);
		
		tbendtime.value = "";
		tbendtime.setAttribute("tooltiptext", tbendtime.value);
	},
	
	loadRows: function()
	{
		// read from pref
		var rows = kwok.downloadpanel.prefs.getCharPref("visibleItems");
		
		if (rows)
		{
			var list = document.getElementById("rowsInfo");
			
			var splitRows = rows.split(",");
			
			for (var i = 0; i < splitRows.length; i++)
			{
				try
				{
					var cur = document.getElementById(splitRows[i]);
					cur.collapsed = false;
					list.insertBefore(cur, null);
				}
				catch (e)
				{
					// ignore as it's probably a missing element
				}
			}
		}
	},
	
	/**
	 * Opens directory with file selected.
	 * Currently not support in *nix.
	 */
	openDirectory: function()
	{
		if (tbpath.value != "")
		{
			var file = nsLocalFile(tbpath.value);
		
			if (file.exists())
				file.reveal();
		}
	},
	
	/**
	 * Opens the URI.
	 */
	openURI: function()
	{
		if (tburi.value != "")
			window.opener.openURL(tburi.value);
	},
	
	/**
	 * Opens a url in a new window/tab.
	 */
	openReferrer: function()
	{
		if (tbref.value != "")
			window.opener.openURL(tbref.value);
	},
}

window.addEventListener("load", kwok.downloadpanel.init, false);